﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SocketTest
{
    class Program
    {
        static void Main(string[] args)
        {

            #region
            //Task.Factory.StartNew(() =>
            //{
            //    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //    var server = new IPEndPoint(IPAddress.Parse("10.1.114.111"), 9999);
            //    client.Connect(server);
            //    int i = 20;
            //    while(i>0)
            //    {
            //        client.Send(Encoding.UTF8.GetBytes("测试消息在" + DateTime.Now + "发送成功！"));
            //        Byte[] byteNum = new Byte[64];
            //        client.Receive(byteNum, byteNum.Length, 0);
            //        Console.WriteLine("服务端：" + Encoding.UTF8.GetString(byteNum));
            //        Thread.Sleep(1000);
            //        i--;
            //    }
            //});
            #endregion
            HttpServerTest();
            Console.ReadKey();
        }


        private static void HttpServerTest()
        {
            Task.Factory.StartNew(() =>
            {
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //在调用 Bind 之前，必须首先创建打算从其进行数据通信的本地 IPEndPoint。
                IPAddress ip = IPAddress.Parse("10.1.114.111");
                serverSocket.Bind(new IPEndPoint(ip, 9999));
                serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
                serverSocket.Listen(10);

                bool flag = true;
                while (flag)
                {
                    //var eventArgs = new SocketAsyncEventArgs();
                    var client = serverSocket.Accept();
                    Thread receiveThread = new Thread(ReceiveMessage);
                    receiveThread.Start(client);
                }
            });

        }

        /// <summary>  
        /// 接收消息  
        /// </summary>  
        /// <param name="clientSocket"></param>  
        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            byte[] result = new byte[1024];
            try
            {
                //通过clientSocket接收数据  
                int receiveNumber = myClientSocket.Receive(result);
                Console.WriteLine("客户端：\r\n");
                Console.Write(Encoding.UTF8.GetString(result, 0, receiveNumber));
                while (receiveNumber == result.Length)
                {
                    receiveNumber = myClientSocket.Receive(result);
                    Console.Write(Encoding.UTF8.GetString(result, 0, receiveNumber));
                }
                myClientSocket.Send(Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\n\r\n"));
                myClientSocket.Send(Encoding.UTF8.GetBytes("客户端消息已收到" + DateTime.Now));
                myClientSocket.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                myClientSocket.Shutdown(SocketShutdown.Both);
                myClientSocket.Close();
            }
        }

        ///// <summary>  
        ///// 监听客户端连接  
        ///// </summary>  
        //private static void ListenClientConnect(Socket serverSocket)
        //{
        //    while (true)
        //    {
        //        Socket clientSocket = serverSocket.Accept();
        //        clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
        //        Thread receiveThread = new Thread(ReceiveMessage);
        //        receiveThread.Start(clientSocket);
        //    }
        //}


    }
}
